package logic

import (
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/stores/sqlc"

	"t-go-zero/service/user/rpc/internal/svc"
	"t-go-zero/service/user/rpc/userclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type ARoleFindOneLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewARoleFindOneLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ARoleFindOneLogic {
	return &ARoleFindOneLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ARoleFindOneLogic) ARoleFindOne(in *userclient.ARoleFindOneReq) (*userclient.ARoleFindOneResp, error) {
	res, err := l.svcCtx.ARoleModel.FindOne(l.ctx, in.Id)
	if err != nil {
		if err == sqlc.ErrNotFound {
			return nil, fmt.Errorf("ARole没有该ID:%v", in.Id)
		}
		return nil, err
	}

	return &userclient.ARoleFindOneResp{
		Id:          res.Id,                    //角色ID
		Name:        res.Name,                  //角色名称
		Remark:      res.Remark.String,         //备注
		CreatedName: res.CreatedName,           //创建人
		CreatedAt:   res.CreatedAt.UnixMilli(), //创建时间
	}, nil
}
